package com.znz.web;

import com.znz.domain.Tcustomer;
import com.znz.domain.Torder;
import com.znz.domain.Treturnvisit;
import com.znz.service.OrderService;
import com.znz.service.ReturnVisitService;
import com.znz.util.common.Page;
import com.znz.util.common.ReturnObj;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * 对回访信息进行操作
 */

@RequestMapping("/returnvisit")
@Controller
public class ReturnVisitController extends AbstractController {

    @Autowired
    ReturnVisitService returnVisitService;

    @Autowired
    OrderService orderService;
    private Logger logger = LoggerFactory.getLogger(ReturnVisitController.class);
    /**
     * h回访记录列表页面根据当前客服id查询，如果为超级管理员则查询出所有
     * @return
     */
    @RequestMapping(value={"/","/returnvisit"})
    public ModelAndView demandList(String userName,Page<Torder> page) {
        try {
            ModelAndView modelAndView=null;
            Map<String,Object> map=new HashMap<String, Object>();
            Long tcustomerId=1l;
            Page<Torder> torderPage=orderService.getTorderAllByTcustomer(tcustomerId,userName,page);
            map.put("torderPage",torderPage);
            modelAndView = getTemplate("returnVisit/list",map);
            return modelAndView;
        } catch (Exception e){
            logger.error("页面初始化异常",e);
            ModelAndView modelAndView = getTemplate("uncaughtException",null);
            return  modelAndView;
        }
    }

    @RequestMapping(value="visit")
    private ModelAndView toEdit(Long visit_id) {
        try {
            //TODO 这里需要根据医院id获取医院对象放到map里
            Map<String,Object> map=new HashMap<String,Object>();
            //TODO 同时还需要获取项目列表放到map里，用于配置项目列表
            ModelAndView modelAndView = getTemplate("returnVisit/visit",map);
            return modelAndView;
        }catch (Exception e){
            logger.error("修改医院页面初始化异常",e);
            ModelAndView modelAndView = getTemplate("uncaughtException",null);
            return  modelAndView;
        }
    }

    /**
     * 方法说明:获取订单所分配的所有医院
     * @param orderId
     * @return
     */
    public ReturnObj getReturnVisitByOrderId(Long orderId){
        //TODO   获取订单所分配的所有医院
        ReturnObj returnObj=null;
        try{
           List<Treturnvisit> treturnvisitList= Treturnvisit.getReturnVisitListByOrderId(orderId);
           if(null!=treturnvisitList && treturnvisitList.size()>0){
              returnObj=ReturnObj.createSuccess(treturnvisitList);
           }
        }catch (Exception e){
          returnObj= ReturnObj.createError();
        }
        return  returnObj;
    }

    /**
     * 方法说明:保存回访信息
     * @return
     */
    public ReturnObj saveReturnVisit(String returnVisit){
        //TODO 保存回访信息
        ReturnObj returnObj=null;
        try {
            JSONObject jsonreturnVisit = JSONObject.fromObject(returnVisit);
            Treturnvisit treturnvisit=returnVisitService.saveTcustomer(jsonreturnVisit);
            returnObj=ReturnObj.createSuccess(treturnvisit);
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            returnObj=ReturnObj.createError();
        }
        return returnObj;
    }

}
